<?php
/**
 * --------------------------------------------------------------
 * index控制器
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

class IndexController extends EntryController
{
    public function __construct()
    {
        parent::__construct();

    }

    /**
     * 商品分类列表
     * @return [type]     [description]
     */
    public function index()
    {
        $this->view();
    }

    /**
     * 获取列表数据
     * @return [type]     [description]
     */
    public function cateList()
    {
        // 判断是否是submit
        is_submit();

        // 条件查询
        $query = [];

        // 商品分类类型
        $search_name = post('search_name', 0);
        if($search_name) {
            $query['name'] =  ['like' => $search_name];
        }

        // 获取where
        $where = get_where($query);

        // 总记录数
        $count = $this->model->getCateCount($where);
        // pt($count);

        if(!empty($count)) {
            // 分页
            $rows = $this->config['LIST_ROW_NUM'] ? $this->config['LIST_ROW_NUM'] : 15;
            // 得到总页数
            $pages = ceil($count/$rows);
            //判断当前页码
            $page = empty($_POST['page']) ? 1 : $_POST['page'];
            $offset = $rows * ($page - 1);
            $limit = $offset . ', ' . $rows;
            // 获取列表信息
            $data = $this->model->getCateList($where, $limit);
            // pt($data);
            if($data) {
                foreach ($data as $k => $v) {
                    // 商品分类头像
                    $data[$k]['show_image'] = default_pictrue($v['cate_image'], $this->config['DEFAULT_EMPTY_PIC']);
                }
            }
        } else {
            $data = '';
            $count = 0;
            $pages = 1;
        }

        json(['status' => 1, 'info' => '获取成功', 'data' => $data, 'count' => $count, 'pages' => $pages]);
    }

    /**
     * 新增商品分类
     * @return [type]     [description]
     */
    public function insertCate()
    {
        // 判断是否是submit
        is_submit();

        // 判断
        $cate_name = post('cate_name', 0);
        if(empty($cate_name)) {
            json(['status' => 0, 'info' => '请输入分类名称', 'code' => 1]);
        }

        // 分类信息入库
        $map = array(
            'name' => $cate_name,
            'image' => post('cate_img', ''),
            'sort' => post('cate_sort', 0),
            'create_time' => TIMESTAMP,
        );
        // pt($map);
        $cate_id = $this->model->insertCate($map);
        if($cate_id === false) {
            json(['status' => 0, 'info' => '新增失败', 'code' => 2]);
        }

        json(['status' => 1, 'info' => '新增成功']);
    }

    /**
     * 删除商品分类
     * @return [type]     [description]
     */
    public function deleteCate()
    {
        // 判断是否是submit
        is_submit();

        // 判断
        $cate_id = post('cate_id', '0');
        if(empty($cate_id)) {
            json(['status' => 0, 'info' => '记录ID不存在', 'code' => 1]);
        }

        // 判断该分类下是否存在商品
        $count = $this->model->getGoodsCountByCateid($cate_id);
        if($count) {
            json(['status' => 0, 'info' => '删除失败，请先删除该分类下面的所有商品', 'code' => 2]);
        }

        // 删除商品分类
        $handle = $this->model->deleteCateById($cate_id);
        if($handle === false) {
            json(['status' => 0, 'info' => '删除失败', 'code' => 4]);
        }

        json(['status' => 1, 'info' => '删除成功']);
    }

    /**
     * 获取商品分类信息
     * @return [type]     [description]
     */
    public function getCateById()
    {
        // 判断是否是submit
        is_submit();

        // 获取记录ID
        $cate_id = post('cate_id', 0);
        if(empty($cate_id)) {
            json(['status' => 0, 'info' => '记录ID不存在', 'code' => 1]);
        }

        // 通过ID获取分类信息
        $info = $this->model->getCateById($cate_id);
        // pt($info);
        if($info === false) {
            json(['status' => 0, 'info' => '记录获取失败', 'code' => 2]);
        }

        // 判断是否默认
        $info['show_image'] = default_pictrue($info['image'], $this->config['DEFAULT_EMPTY_PIC']);

        json(['status' => 1, 'info' => '获取成功', 'data' => $info]);
    }

    /**
     * 更新商品分类信息
     * @return [type]     [description]
     */
    public function updateCate()
    {
        // 判断是否是submit
        is_submit();

        // 判断
        $cate_id = post('cate_id', 0);
        if(empty($cate_id)) {
            json(['status' => 0, 'info' => '商品分类ID错误', 'code' => 1]);
        }
        $cate_name = post('cate_name', 0);
        if(empty($cate_name)) {
            json(['status' => 0, 'info' => '请输入分类名称', 'code' => 2]);
        }

        // 分类信息入库
        $map = array(
            'name' => $cate_name,
            'image' => post('cate_img', ''),
            'sort' => post('cate_sort', 0),
            'create_time' => TIMESTAMP,
        );
        // pt($map);
        $handle = $this->model->updateCate($map, 'id = ' . $cate_id);
        if($handle === false) {
            json(['status' => 0, 'info' => '修改失败', 'code' => 3]);
        }

        json(['status' => 1, 'info' => '修改成功']);
    }
}